﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace ChessBoard
{
    class ChessClock
    {
        public const int Left = 24;
        public const int Top = 1;
        public PlayerTime WhiteTime { get; set; }
        public PlayerTime BlackTime { get; set; }

        public ChessClock(int hours, int minutes, int seconds)
        {
            this.WhiteTime = new PlayerTime()
            {
                Hours = hours,
                Minutes = minutes,
                Seconds = seconds
            };

            this.BlackTime = new PlayerTime()
            {
                Hours = hours,
                Minutes = minutes,
                Seconds = seconds
            };
        }

        public void UpdateClock(Player player)
        {
            if (player == Player.White)
            {
                DrawClock(ChessClock.Left, ChessClock.Top, this.WhiteTime, ConsoleColor.Black, ConsoleColor.White);
            }
            else
            {
                DrawClock(ChessClock.Left + 32, ChessClock.Top, this.BlackTime, ConsoleColor.Black, ConsoleColor.DarkGray);
            }
        }
        
        public void Draw()
        {            
            // Draws White clock
            DrawClock(ChessClock.Left, ChessClock.Top, this.WhiteTime, ConsoleColor.Black, ConsoleColor.White);
            // Draws Black clock
            DrawClock(ChessClock.Left + 32, ChessClock.Top, this.BlackTime, ConsoleColor.Black, ConsoleColor.DarkGray);
        }

        private void DrawClock(int x, int y, PlayerTime pTime, ConsoleColor bgColor, ConsoleColor fgColor)
        {
            int left = Console.CursorLeft;
            int top = Console.CursorTop;
            ConsoleColor currentFgColor = Console.ForegroundColor;

            Console.BackgroundColor = bgColor;
            Console.ForegroundColor = fgColor;

            int hoursFirstDigit = (pTime.Hours / 10) % 10;
            int hoursSecondDigit = pTime.Hours % 10;
            int minFirstDigit = (pTime.Minutes / 10) % 10;
            int minSecondDigit = pTime.Minutes % 10;
            int secFirstDigit = (pTime.Seconds / 10) % 10;
            int secSecondDigit = pTime.Seconds % 10;
            // Hours
            ClockDigit.Draw(hoursFirstDigit, x, y);
            ClockDigit.Draw(hoursSecondDigit, x + 3, y);
            Console.SetCursorPosition(x + 7, y);
            Console.Write("▄");
            Console.SetCursorPosition(x + 7, y + 2);
            Console.Write("▀");
            // Minutes
            ClockDigit.Draw(minFirstDigit, x + 9, y);
            ClockDigit.Draw(minSecondDigit, x + 12, y);
            Console.SetCursorPosition(x + 16, y);
            Console.Write("▄");
            Console.SetCursorPosition(x + 16, y + 2);
            Console.Write("▀");
            // Seconds
            ClockDigit.Draw(secFirstDigit, x + 18, y);
            ClockDigit.Draw(secSecondDigit, x + 21, y);
            // Moving the cursor to the MoveEntry field
            Console.SetCursorPosition(left, top);
            Console.ForegroundColor = currentFgColor;
        }
    }
}
